/*
 * gui.cc
 */

#include "gui.h"
#include "core.h"

using namespace std;

namespace newsclient {

GUI::GUI() {
	printWelcome();

	//Insert availible commands into cmds vector
	addCommand("help", "Display this menu");
	addCommand("commands", "Displays this menu");
	addCommand("delete", "Delete an item, (newsgroup/article)");
	addCommand("add", "Add an newsgroup/article");
	addCommand("list", "list newsgroups/articles");
	addCommand("get", "displays an article");
	addCommand("enter", "Enter a newsgroup");
	addCommand("leave", "Leave current newsgroup");
	addCommand("quit", "Exit program");
}

GUI::~GUI() {
	//nothing for now
}

void GUI::input() {
	cout << "\033[32m> \033[0m";
	while(parse()) {
		cout << "\033[32m";
		if(core->getState() == 1) cout << core->getGroupName();
		cout << "> \033[0m";
	}
}

void GUI::quit() {
	cout << "Shutting down" << endl;
	//    cout << "Press ENTER to quit." << endl;
}


void GUI::setCore(Core* c) {
	core = c;
}

void GUI::printCommands() const {
	cout << "Commands: " << endl;
	for(size_t i = 0;i < cmds.size(); ++i) {
		cout << "\t" << cmds[i].first;
		if(cmds[i].first.length() < 7) cout << "\t";
		cout <<"\t:" << cmds[i].second << endl;
	}
}

void GUI::addCommand(string command, string desc) {
	pair<string, string> tmp(command, desc);
	cmds.push_back(tmp);
}

void GUI::printWelcome() const {
	cout << "Welcome to the News Client!" << endl;
	cout << "Type 'help' for help." << endl;
}


bool GUI::parse() {
	string input, arg1, arg2;
	//      istringstream iss(in_str, istringstream::in);
	cin >> input;

	// state independant commands
	if(input == "quit") {
		core->quit();
		return false;
	}
	else if(input == "help" || input == "commands") {
		printCommands();
		return true;
	}
	// state dependant commands
	else {
		switch(core->getState()) {
		case 0:                   // At root
			if(input == "list") {
				core->listNewsgroups();
				return true;
			}
			else if(input == "add") {
				cout << "Enter newsgroup name:" << endl;
				cin >> arg1;
				core->addNewsgroup(arg1);
				return true;
			}
			else if(input == "delete") {
				cout << "Enter a newsgroup ID:" << endl;
				cin >> arg1;
				core->deleteNewsgroup(arg1);
				return true;
			}
			else if(input == "enter") {
				cout << "Enter a newsgroup ID:" << endl;
				cin >> arg1;
				core->enterNewsgroup(arg1);
				return true;
			}
			else { 	cout << "Unknown command" << endl; }
			break;
		case 1:
			if(input == "list") {
				core->listArticles();
				return true;
			}
			else if(input == "add") {
				cin.ignore(1);
				fflush(stdin);
				cout << "Enter author name:" << endl;
				getline(cin, arg1);
				cout << "Enter article title:" << endl;
				getline(cin, arg2);
				core->addArticle(arg1, arg2);
				return true;
			}
			else if(input == "delete") {
				cout << "Enter an article ID:" << endl;
				cin >> arg1;
				core->deleteArticle(arg1);
				return true;
			}
			else if(input == "get") {
				cout << "Enter an article ID:" << endl;
				cin >> arg1;
				core->getArticle(arg1);
				return true;
			}
			else if(input == "leave") {
				core->leaveNewsgroup();
				return true;
			}
			else { 	cout << "Unknown command" << endl; }
			break;
		}

	}
	fflush(stdin);
	return true;
}
}
